#!/bin/bash


stop_containers() {
    for i in $(sudo docker ps | grep ${1} | cut -f1 -d" ")
        do
            echo "> stop container $i"
            sudo docker stop $i
    done
}

remove_containers() {
    for i in $(sudo docker ps -a | grep ${1} | cut -f1 -d" ")
        do
            echo "> remove container $i"
            sudo docker rm $i
    done
}

# build_mms_container() {
#     sudo docker run --name ${1} -it -d \
#     -p 8080:8080 \
#     --link kissnapp_redis:redis_host \
#     --link kissnapp_mysql_server:mysql_host \
#     -v ${2}:/home/app/mmserl ubuntu \
#     /home/app/mmserl/mms/bin/mms console
# }

build_mms_container() {
    sudo docker run --name ${1} -it -d \
    -p 8080:8080 \
    -v ${2}:/home/app/mmserl ubuntu \
    /home/app/mmserl/mms/bin/mms console
}

# build_mongooseim_container() {
#     sudo docker run --name ${1} -it -d \
#     -p 5222:5222 \
#     -p 5269:5269 \
#     --link kissnapp_redis:redis_host \
#     --link kissnapp_mysql_server:mysql_host \
#     -v ${2}:/home/app/mongooseim ubuntu \
#     /home/app/mongooseim/bin/mongooseimctl live
# }

build_mongooseim_container() {
    sudo docker run --name ${1} -it -d \
    -p 5222:5222 \
    -p 5269:5269 \
    -v ${2}:/home/app/mongooseim ubuntu \
    /home/app/mongooseim/bin/mongooseimctl live
}

start_container() {
    sudo docker start ${1}
}

check_container() {
    containers=$(sudo docker ps | grep ${1} | cut -f1 -d" ")
    echo $containers
    if [ ${#containers} = 0 ]; then
        echo "> failed to run container ${1}!"
        exit 1
    else
        echo "> run container ${1} success!"
    fi
}

show_containers() {
    sudo docker ps -a
}

show_running_containers() {
    sudo docker ps
}

rollback() {
    echo "> stop running containers..."
    stop_containers ${2}

    echo "> start containers ${1}"
    start_container ${1}

    check_container ${1}
}